﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Linq;

namespace Cheetah_Fast_Banking_App
{
    public class Account
    {
        #region Private Variables
        private string _guid;
        private string _accountName;
        private string _passwordHash;
        #endregion // Private Variables

        #region Properties
        public ObservableCollection<Transaction> Transactions { get; set; }

        /// <summary>
        /// Unique Identifier for account
        /// </summary>
        public string Guid
        {
            get { return _guid; }
            set { _guid = value; }
        }

        public string AccountName
        {
            get { return _accountName; }
            set { _accountName = value; }
        }

        public string PasswordHash
        {
            get { return _passwordHash; }
            set { _passwordHash = value; }
        }
        #endregion // Properties

        #region Constructors
        /// <summary>
        /// Default Constructor
        /// </summary>
        public Account()
        {
            Transactions = new ObservableCollection<Transaction>();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="guid">Unique ID for account</param>
        public Account(string guid) : this()
        {
            _guid = guid;
        }
        #endregion // Constructors

        #region Methods

        /// <summary>
        /// Save a new Transaction to the current account
        /// </summary>
        /// <param name="transaction">Transaction to be saved</param>
        public void SaveNewTransaction(Transaction transaction)
        {
            string filePath = DataSourceSingleton.GetInstance().XmlFile;
            XDocument doc = XDocument.Load(filePath);
            XElement newElement = new XElement("transaction",
                                                new XAttribute("id", transaction.ID),
                                                new XAttribute("date", transaction.Date),
                                                new XAttribute("description", transaction.Description),
                                                new XAttribute("amount", transaction.Amount));
            XElement accountElement = (from e in doc.Descendants("account")
                                       where e.Attribute("name").Value == _accountName
                                       select e).Single();
            accountElement.Descendants("transactions").Last().Add(newElement);
            doc.Save(filePath);
            Transactions.Add(transaction);
            transaction.SetCurrentBalance(Transactions.Last().CurrentBalance);
        }

        /// <summary>
        /// Deletes a Transaction from the current account
        /// </summary>
        /// <param name="transaction">Transaction to be deleted</param>
        public void DeleteTransaction(Transaction transaction)
        {
            string filePath = DataSourceSingleton.GetInstance().XmlFile;
            XDocument doc = XDocument.Load(filePath);
            XElement transactionElement = (from t in doc.Descendants("transaction")
                                           where t.Attribute("id").Value == transaction.ID.ToString()
                                           select t).Single();
            transactionElement.Remove();
            doc.Save(filePath);
            Transactions.Remove(transaction);
            // TODO: Compute current balance from remaining transaction objects?
        }
        #endregion // Methods
    }
}
